<template>
  <Modal
    v-model="form.visible"
    :width="1000"
    :mask-closable="false"
    :loading="loading"
    :title="form.data.id ? '修改兑换商品' : '新增兑换商品'"
    ok-text="提交"
    @on-ok="ok">
    <Row class="mt30">
      <Col span="16" offset="4">
        <Form ref="form" :model="form.data" :rules="ruleValidate" :label-width="110">
          <FormItem prop="photoPath" label="兑换商品图片">
            <upload-img v-if="form.visible" @on-change="onUploadImg" :defaultImageList="defaultImageList"></upload-img>
          </FormItem>
          <FormItem prop="srvProName" label="兑换商品名称">
            <Input v-model="form.data.srvProName" placeholder="请输入"></Input>
          </FormItem>
          <FormItem prop="needScore" label="兑换商品积分">
            <InputNumber v-model.number="form.data.needScore" placeholder="请输入"></InputNumber>
          </FormItem>
          <FormItem prop="totalCount" label="兑换商品数量">
            <InputNumber v-model.number="form.data.totalCount" placeholder="请输入"></InputNumber>
          </FormItem>
          <FormItem prop="isEnable" label="是否外网显示">
            <Select v-model="form.data.isEnable" style="width:200px">
              <Option :value="'0'">显示</Option>
              <Option :value="'1'">不显示</Option>
            </Select>
          </FormItem>
          <FormItem prop="srvProMemo" label="兑换商品说明">
            <Input type="textarea" v-model="form.data.srvProMemo" placeholder="请输入" :rows="4"></Input>
          </FormItem>
        </Form>
      </Col>
    </Row>
  </Modal>
</template>

<script>
  import Vue from 'vue'
  import { mapGetters } from 'vuex'
  import uploadImg from '@/components/upload-img'
  import vipApi from '@/api/vip'

  export default {
    name: 'update-modal',
    props: [
      'form'
    ],
    components: {
      uploadImg
    },
    data () {
      return {
        loading: true,
        ruleValidate: {
          photoPath: [
             { required: true, message: '请上传兑换商品图片', trigger: 'change' }
          ],
          srvProName: [
            { required: true, message: '请输入兑换商品名称', trigger: 'change' },
            { max: 50, message: '兑换商品名称字符长度应小于50', trigger: 'change' }
          ],
          needScore: [
            { required: true, type: 'number', message: '请输入兑换商品积分', trigger: 'change' }
          ],
          totalCount: [
            { required: true, type: 'number', message: '请输入兑换商品数量', trigger: 'change' }
          ],
          isEnable: [
            { required: true, message: '请选择是否外网显示', trigger: 'change' }
          ],
          srvProMemo: [
            { max: 100, message: '兑换商品说明字符长度应小于100', trigger: 'change' }
          ]
        }
      }
    },
    computed: {
      ...mapGetters({
        userInfo: 'userInfo'
      })
    },
    watch: {
      'form.visible': 'onClose'
    },
    methods: {
      ok () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.submitForm()
          } else {
            this.$Message.error('表单验证失败，请检查输入项目!')
            this.form.visible = true
            this.loading = false
            setTimeout(() => {
              this.loading = true
            }, 100)
          }
        })
      },
      onClose (isVisible) {
        if (!isVisible) {
          this.$nextTick(() => {
            this.$refs.form.resetFields()
          })
        } else {
          this.defaultImageList = Vue.filter('splitImg')(this.form.data.photoPath || '')
        }
      },
      onUploadImg (imgList) {
        this.form.data.photoPath = imgList.filter((item) => {
          return item.name.length > 0
        }).map((item) => {
          if (item.response && item.response.responseData) {
            return item.response.responseData
          } else {
            return item.name
          }
        }).join(',')
        this.$refs.form.validateField('photoPath')
      },
      async submitForm () {
        this.form.data.pkRcId = this.userInfo.pkRclubInfo
        if (this.form.data.id) {
          const { body } = await vipApi.updateGoods(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('修改成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        } else {
          const { body } = await vipApi.createGoods(this.form.data)
          if (this.$apiHelper.requestSuccessed(body.responseCode)) {
            this.$Message.info('创建成功')
          } else {
            setTimeout(() => {
              this.$Modal.error({
                title: '错误',
                content: body.desc,
                onOk: () => {}
              })
            }, 500)
          }
        }
        this.form.visible = false
        this.$emit('submit')
      }
    }
  }
</script>
